#include "mainwindow.h"
QStandardItemModel *model= new QStandardItemModel(1,1);
int i=-1;

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    connect(&close, SIGNAL(clicked()), this, SLOT(close()));
    connect(&but1, SIGNAL(clicked()), this, SLOT(openfile()));
    connect(&spis, SIGNAL(currentIndexChanged(QString)), this, SLOT(write()));
    connect(&save, SIGNAL(clicked()), this, SLOT(saving()));
}

MainWindow::~MainWindow()
{

}
void MainWindow::openfile()
{
    Dr=QFileDialog::getExistingDirectory(this,"Open Directory");
    QDir Dir(Dr);
    QStringList list;
            list<<"*.txt";
    QStringList sl=Dir.entryList(list);
    spis.addItems(sl);
}
void MainWindow::write()
{
    QFile file((Dr+char(92)+spis.currentText()));
    file.open(QIODevice::ReadWrite | QIODevice::Text);
    QTextStream in(&file);
    QString txt;
    model->setRowCount(0);
    txt=in.readLine();
    i=-1;
    while (txt!="")
    {
        QStandardItem *firstRow = new QStandardItem(txt);
        i++;
        model->setItem(i,0,firstRow);
        txt=in.readLine();
    }
    file.close();
    model->setHorizontalHeaderItem(0,new QStandardItem(QString("Text")));
    text.setModel(model);
}
void MainWindow::saving()
{
    QFile file((Dr+char(92)+spis.currentText()));
    file.open(QIODevice::ReadWrite | QIODevice::Text);
    QTextStream out(&file);
    for(int j=0; j<=i; j++)
    {
        QStandardItem *it=model->item(j);
        QString ab=it->text();
        out<<ab<<"\n";
    }
    file.close();
}
